package com.javaclimb.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javaclimb.music.dao.ConsumerMapper;
import com.javaclimb.music.domain.Consumer;
import com.javaclimb.music.service.ConsumerService;
import com.javaclimb.music.utils.PageUtils;
import com.javaclimb.music.utils.Query;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    @Override
    public Consumer verifyPassword(String username, String password) {
        return this.getOne(new LambdaQueryWrapper<Consumer>()
                .eq(Consumer::getUsername, username)
                .eq(Consumer::getPassword, password));
    }

    @Override
    public Consumer getByUsername(String username) {
        return this.getOne(new LambdaQueryWrapper<Consumer>().eq(Consumer::getUsername, username));
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<Consumer> wrapper = new LambdaQueryWrapper<>();

        //是否进行分页
        boolean isPage = false;
        if (StringUtils.isNotBlank((CharSequence) params.get("isPage"))) {
            isPage = Boolean.parseBoolean(String.valueOf(params.get("isPage")));
        }
        PageUtils pageUtils = null;
        if (isPage) {
            IPage<Consumer> page = this.page(new Query<Consumer>().getPage(params), wrapper);
            pageUtils = new PageUtils(page);
        } else {
            List<Consumer> list = this.list(wrapper);
            pageUtils = new PageUtils(list, 0, 0, 0);
        }
        return pageUtils;
    }
}
